{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 环境部署"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Platform : linux [win32/linux]\n",
      "Systerm  : 3.5.2 (default, Nov 23 2017, 16:37:01) \n",
      "[GCC 5.4.0 20160609] \n",
      "当前时间：20201021 18:17:04\n",
      "当前路径: /notebooks/Desktop\n"
     ]
    }
   ],
   "source": [
    "import time \n",
    "import os \n",
    "import sys\n",
    "\n",
    "print('Platform : {} [win32/linux]'.format(sys.platform))  # 当前平台信息 \n",
    "print('Systerm  : {} '.format(sys.version))\n",
    "print('当前时间：{}'.format(time.strftime('%Y%m%d %H:%M:%S', time.localtime(time.time()))))\n",
    "print('当前路径: {}'.format(os.path.abspath(os.path.curdir)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 记录当前环境 所有依赖包及其精确的版本号。以便新环境部署。  \n",
    "\n",
    "### 生成requirements.txt文件\n",
    " pip freeze > requirements.txt\n",
    " \n",
    "### 安装requirements.txt依赖\n",
    " pip install -r requirements.txt\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Uninstalling jieba-0.42.1:\n",
      "  Successfully uninstalled jieba-0.42.1\n"
     ]
    }
   ],
   "source": [
    "# 卸载包  -y 代表默认选择 yes\n",
    "! pip uninstall jieba -y "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jieba==0.42.1"
     ]
    }
   ],
   "source": [
    "! cat requirements.txt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting jieba==0.39\n",
      "\u001b[?25l  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/71/46/c6f9179f73b818d5827202ad1c4a94e371a29473b7f043b736b4dab6b8cd/jieba-0.39.zip (7.3MB)\n",
      "\u001b[K    100% |████████████████████████████████| 7.3MB 3.6MB/s eta 0:00:01\n",
      "\u001b[?25hBuilding wheels for collected packages: jieba\n",
      "  Running setup.py bdist_wheel for jieba ... \u001b[?25ldone\n",
      "\u001b[?25h  Stored in directory: /root/.cache/pip/wheels/91/6d/81/a6e61000fce4618fe8625512ffd267969a5c3b18759eaac37c\n",
      "Successfully built jieba\n",
      "Installing collected packages: jieba\n",
      "Successfully installed jieba-0.39\n",
      "\u001b[33mYou are using pip version 18.1, however version 20.2.4 is available.\n",
      "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# 安装 包\n",
    "! pip install jieba==0.39 -i  https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jieba==0.39\n"
     ]
    }
   ],
   "source": [
    "! pip freeze | grep jieba "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting jieba==0.42.1 (from -r requirements.txt (line 1))\n",
      "Installing collected packages: jieba\n",
      "  Found existing installation: jieba 0.39\n",
      "    Uninstalling jieba-0.39:\n",
      "      Successfully uninstalled jieba-0.39\n",
      "Successfully installed jieba-0.42.1\n",
      "\u001b[33mYou are using pip version 18.1, however version 20.2.4 is available.\n",
      "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# 安装 包\n",
    "! pip install -r requirements.txt  -i  https://pypi.tuna.tsinghua.edu.cn/simple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jieba==0.42.1\n"
     ]
    }
   ],
   "source": [
    "! pip freeze | grep jieba "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.douban.com/simple\n",
      "Requirement already up-to-date: jieba in /usr/local/lib/python3.5/dist-packages (0.42.1)\n",
      "\u001b[33mYou are using pip version 18.1, however version 20.2.4 is available.\n",
      "You should consider upgrading via the 'pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "# 更新包\n",
    "! pip install --upgrade jieba -i https://pypi.douban.com/simple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Python使用conda安装requirement.txt的扩展包](https://blog.csdn.net/weixin_45092662/article/details/106906719)  \n",
    "\n",
    "### 本地打开 jupyter lab:  \n",
    "jupyter lab --notebook-dir=D:\\   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--- \n",
    "### 导出conda环境，导出格式为.yml文件   \n",
    "conda env export > requirements.yml\n",
    "### 根据 requirements.yml 创建conda 环境\n",
    "conda env create -f requirements.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name: py37_Base\n",
      "channels:\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/\n",
      "  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo/\n",
      "  - defaults\n",
      "dependencies:\n",
      "- _libgcc_mutex=0.1=main\n",
      "- ca-certificates=2020.10.14=0\n",
      "- certifi=2020.6.20=py37_0\n",
      "- ld_impl_linux-64=2.33.1=h53a641e_7\n",
      "- libedit=3.1.20191231=h14c3975_1\n",
      "- libffi=3.3=he6710b0_2\n",
      "- libgcc-ng=9.1.0=hdf63c60_0\n",
      "- libstdcxx-ng=9.1.0=hdf63c60_0\n",
      "- ncurses=6.2=he6710b0_1\n",
      "- openssl=1.1.1h=h7b6447c_0\n",
      "- pip=20.2.4=py37_0\n",
      "- python=3.7.9=h7579374_0\n",
      "- readline=8.0=h7b6447c_0\n",
      "- setuptools=50.3.0=py37hb0f4dca_1\n",
      "- sqlite=3.33.0=h62c20be_0\n",
      "- tk=8.6.10=hbc83047_0\n",
      "- wheel=0.35.1=py_0\n",
      "- xz=5.2.5=h7b6447c_0\n",
      "- zlib=1.2.11=h7b6447c_3\n",
      "prefix: /home/vincent/anaconda3/envs/py37_Base\n",
      "\n"
     ]
    }
   ],
   "source": [
    "! cat environment.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---\n",
    "#  [Redis](https://pypi.org/project/redis-py-cluster/)\n",
    "[Docker 安装 Redis](https://www.runoob.com/docker/docker-install-redis.html)  \n",
    "[Linux_基于Docker搭建Redis集群](https://segmentfault.com/a/1190000010131816)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: redis-py-cluster in c:\\programdata\\anaconda3\\lib\\site-packages (2.1.0)\n",
      "Requirement already satisfied: redis<4.0.0,>=3.0.0 in c:\\programdata\\anaconda3\\lib\\site-packages (from redis-py-cluster) (3.5.3)\n"
     ]
    }
   ],
   "source": [
    "! pip install redis-py-cluster -i  https://pypi.tuna.tsinghua.edu.cn/simple\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Redis 调用程序 \n",
    "class myRedis(object):\n",
    "    def __init__(self, redis_type=None, **args):\n",
    "        if redis_type == \"cluster\":\n",
    "            import rediscluster\n",
    "            print('Cluster   Version:{}'.format(rediscluster.__version__))\n",
    "#             self.r_conn = rediscluster.StrictRedisCluster(**args)\n",
    "            self.r_conn = rediscluster.RedisCluster(**args)\n",
    "        else:\n",
    "            import redis\n",
    "            print('Single    Version:{}'.format(redis.__version__))\n",
    "            self.r_conn = redis.StrictRedis(**args)\n",
    "\n",
    "    def IncrValue(self, key):\n",
    "        return self.r_conn.incr(key)\n",
    "    \n",
    "    def GetSetValue(self, key, value):\n",
    "        return self.r_conn.getset(key, value)\n",
    "\n",
    "    def SetValue(self, key, value):\n",
    "        \"\"\"\n",
    "        设置 某个 key 的值 \n",
    "        \"\"\"\n",
    "        self.r_conn.set(key, value)\n",
    "  \n",
    "    def GetValue(self, key):\n",
    "        \"\"\"\n",
    "        获取 某个 key 的值 \n",
    "        \"\"\"\n",
    "        return self.r_conn.get(key)\n",
    "\n",
    "        \n",
    "    def Expire(self, key, seconds):\n",
    "        \"\"\"\n",
    "        给某个 key 设置过期时间\n",
    "        \"\"\"\n",
    "        return self.r_conn.expire(key, seconds)\n",
    "    \n",
    "    def Delete(self, key):\n",
    "        \"\"\"\n",
    "        删除某个 key\n",
    "        \"\"\"\n",
    "        return self.r_conn.delete(key)\n",
    "    \n",
    "    def AllKeys(self, prefix='*'):\n",
    "        \"\"\"\n",
    "        Redis 中的所有数据 Keys\n",
    "        prefix 检索规则\n",
    "        \"\"\"       \n",
    "        return self.r_conn.keys(prefix)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Single    Version:3.5.3\n",
      "获取值： test1\n",
      "已过期： None\n",
      "\n",
      "获取值： test2\n",
      "删除后： None\n",
      "\n",
      "获取值： 测试3\n"
     ]
    }
   ],
   "source": [
    "#  docker run --rm -it --name redis_demo -p 6380:6379 redis --requirepass 1234567\n",
    "\n",
    "#单点\n",
    "conn_dict={\"host\": \"192.168.214.66\", \"port\": 6380, 'decode_responses': True, 'password': '1234567'}\n",
    "redis_type='single'\n",
    "myredis = myRedis(redis_type,**conn_dict)\n",
    "\n",
    "# ####################################################\n",
    "# 设置值\n",
    "myredis.SetValue('name','test1')\n",
    "print('获取值：', myredis.GetValue('name'))\n",
    "\n",
    "# 设置过期时间\n",
    "myredis.Expire('name',1)\n",
    "import time\n",
    "time.sleep(1)\n",
    "print('已过期：', myredis.GetValue('name'))\n",
    "\n",
    "# ####################################################\n",
    "# 设置值\n",
    "myredis.SetValue('name2','test2')\n",
    "print('\\n获取值：', myredis.GetValue('name2'))\n",
    "# 删除值 \n",
    "myredis.Delete('name2')\n",
    "print('删除后：', myredis.GetValue('name2'))\n",
    "\n",
    "# ####################################################\n",
    "# 设置值\n",
    "myredis.SetValue('name3','测试3')\n",
    "print('\\n获取值：', myredis.GetValue('name3'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "获取值： 测试3\n"
     ]
    }
   ],
   "source": [
    "print('获取值：', myredis.GetValue('name3'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "所有数据： ['Demo数据', 'name3']\n",
      "筛选数据： ['name3']\n"
     ]
    }
   ],
   "source": [
    "myredis.SetValue('Demo数据','test4')\n",
    "\n",
    "print('所有数据：', myredis.AllKeys())\n",
    "\n",
    "print('筛选数据：', myredis.AllKeys('*ame*'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# # cluster  集群  (未调试成攻，暂时搁置)\n",
    "# conn_dict = {\"startup_nodes\": [{'host': '192.168.0.100', 'port': 6379},\n",
    "#                                {'host': '192.168.0.100', 'port': 6380},\n",
    "#                                {'host': '192.168.0.100', 'port': 6381}]}\n",
    "# redis_type = 'cluster'\n",
    "# myredis = myRedis(redis_type, **conn_dict)\n",
    "# print(myredis.SetValue('name', 'test'))\n",
    "# print(myredis.GetValue('name'))\n",
    "# print(myredis.GetSetValue('name', 0))\n",
    "# print(myredis.GetValue('name'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
